namespace SubhadraSolutions.Sharp.Logging.LogSubscribers.FileSystem
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;

    public class TextFileLogSubscriber : AbstractFileLogSubscriber
    {
        #region Fields

        private string _linePadding = "\r\n";

        #endregion Fields

        #region Properties

        public string LinePadding
        {
            get { return _linePadding; }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    _linePadding = "\r\n";
                }
                else
                {
                    value = value.Trim().Trim('\r', '\n');
                    _linePadding = "\r\n" + value + "\r\n";
                }
            }
        }

        #endregion Properties

        #region Methods

        protected override void publishLogs(StreamWriter writer, IEnumerable<LogStoreItem> e,
            bool isNewFile)
        {
            foreach (LogStoreItem logStoreItem in e)
            {
                _logItemIndex++;
                writer.Write(_logItemIndex.ToString(CultureInfo.InvariantCulture));
                writer.Write("\t");
                if (string.IsNullOrEmpty(ProcessInstanceName))
                {
                    logStoreItem.WriteToWriter(writer);
                }
                else
                {
                    writer.Write(ProcessInstanceName);
                    writer.Write("\t");
                    logStoreItem.WriteToWriter(writer);
                }
                writer.Write(_linePadding);
            }
        }

        #endregion Methods
    }
}